﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using RimWorld;
using UnityEngine;
using Verse;

namespace ARMIR
{
    public class speDraw : GameComponent
    {
        public speDraw(Game game) { } //初始化组件
        public static List<ARMIR_dragLight> activeLight = new List<ARMIR_dragLight>();
        public static List<ARMIR_blockBox> activeBlock = new List<ARMIR_blockBox>();

        public override void LoadedGame()
        {
            base.LoadedGame();
            activeLight = new List<ARMIR_dragLight>();
            activeBlock = new List<ARMIR_blockBox>();
        }

        public override void GameComponentTick()
        {
            base.GameComponentTick();
            drawDragLight();
            drawActivedBlockBox();
        }

        private void drawDragLight() {
            if (activeLight.Count > 0) {
                List<int> clearList = new List<int>();
                //每一根光柱的进度绘制
                for (int i = 0; i < activeLight.Count; i++) { 
                    ARMIR_dragLight acLight = activeLight[i];
                    Material whiteMat = SolidColorMaterials.SimpleSolidColorMaterial(new Color(0.302f, 0.941f, 0.914f, 1f));
                    if (acLight.progress <= 1) {
                        //线性插值progress绘制线条
                        Vector3 B = Vector3.Lerp(acLight.enterPos, acLight.landPos, acLight.progress);
                        GenDraw.DrawLineBetween(acLight.enterPos,B,whiteMat,acLight.width);
                        //进度+1
                        acLight.progress = acLight.progress + acLight.extendSpeed;
                        activeLight[i] = acLight;
                    }else { //已抵达后开始缩小
                        if (acLight.outDelay > 0) { 
                            //有延迟则延迟结束后才开始缩小
                            acLight.outDelay = acLight.outDelay - 1;
                            GenDraw.DrawLineBetween(acLight.enterPos, acLight.landPos, whiteMat, acLight.width);
                            activeLight[i] = acLight;
                            continue;
                        }
                        if (acLight.disProgress > 1) {
                            //续出下一段光柱
                            if (acLight.backLight) {
                                if (acLight.backDelay > 0) { 
                                    //延迟生成倒计时
                                    acLight.backDelay = acLight.backDelay - 1;
                                    activeLight[i] = acLight;
                                    continue;
                                }else { //生成下一段光柱
                                    DragLight(
                                        acLight.map,
                                        acLight.landPos,
                                        acLight.outDelay,
                                        acLight.extendSpeed,
                                        25,
                                        false,
                                        true,
                                        0,
                                        acLight.width
                                        );
                                    clearList.Add(i);
                                    continue;
                                }
                            }
                            //如果结束加入清理名单
                            clearList.Add(i);
                            continue;
                        } 
                        //线性插值disProgress绘制线条
                        Vector3 B = Vector3.Lerp(acLight.enterPos, acLight.landPos, acLight.disProgress);
                        GenDraw.DrawLineBetween(B,acLight.landPos, whiteMat, acLight.width);
                        //进度+1
                        acLight.disProgress = acLight.disProgress + acLight.extendSpeed / 2;
                        activeLight[i] = acLight;
                    }
                }
                //清理
                if (clearList.Count > 0) {
                    for (int i = 0; i < clearList.Count; i++) {
                        activeLight.RemoveAt(clearList[i]);
                    }
                }
            }
        }
        /// <summary>
        /// 激活一根光柱，抵达后收回(外部使用接口)
        /// </summary>
        public static void DragLight(Map map,Vector3 landPos,int outDelay,float extendSpeed,float Len,bool backLight,bool isback,float backDelay,float width) {
            //随机生成入点以及出点
            float altitude = AltitudeLayer.MetaOverlays.AltitudeFor();
            Vector3 enterPos = new Vector3(landPos.x + Rand.Range(-3,3), altitude, landPos.z + Len + Rand.Range(-1,1));
            Vector3 landPOS = new Vector3(landPos.x, altitude, landPos.z);

            if (isback) {
                var a = enterPos;
                enterPos = landPOS;
                landPOS = a;
            }

            ARMIR_dragLight newLight = new ARMIR_dragLight(
                map,
                landPOS,
                enterPos,
                outDelay,
                extendSpeed,
                0,
                0,
                backLight,
                backDelay,
                width
                );
            activeLight.Add(newLight); 
        }
        /// <summary>
        /// 光柱数据结构体
        /// </summary>
        public struct ARMIR_dragLight {
            public Map map; //所在地图
            public Vector3 landPos; //坐标地点
            public Vector3 enterPos; //高点出发位
            public int outDelay; //光柱消失延迟
            public float extendSpeed; //光柱速度
            public float disProgress; //消失进度
            public float progress; //进度
            public bool backLight; //光柱完成后生成返回光柱
            public float backDelay; //生成返回光柱的延迟
            public float width; //光柱宽度

            public ARMIR_dragLight(Map map, Vector3 landPos, Vector3 enterPos, int outDelay, float extendSpeed, float disProgress, float progress,bool backLight,float backDelay,float width) : this()
            {
                this.map = map;
                this.landPos = landPos;
                this.enterPos = enterPos;
                this.outDelay = outDelay;
                this.extendSpeed = extendSpeed;
                this.disProgress = disProgress;
                this.progress = progress;
                this.backLight = backLight;
                this.backDelay = backDelay;
                this.width = width;
            }
        }

        private void drawActivedBlockBox() {
            if (activeBlock.Count > 0) { 
                List<int> clearListA = new List<int>();
                float altitude = AltitudeLayer.MetaOverlays.AltitudeFor();
                Material whiteMat = SolidColorMaterials.SimpleSolidColorMaterial(new Color(1f, 1f, 1f, 0.8f));
                for (int i = 0; i < activeBlock.Count; i++) { 
                    var acBlock = activeBlock[i];
                    //绘制
                    Vector3 pointA = new Vector3(acBlock.progress.x ,altitude, acBlock.progress.z - acBlock.height / 2);
                    Vector3 pointB = new Vector3(acBlock.progress.x, altitude, acBlock.progress.z + acBlock.height / 2);
                    GenDraw.DrawLineBetween(pointA, pointB, whiteMat, acBlock.width);
                    //增加进度
                    Vector3 newPos = new Vector3(acBlock.progress.x, acBlock.progress.y, acBlock.progress.z + acBlock.speed);
                    //如果超出长度则删除
                    if (Math.Abs(newPos.z - acBlock.doPos.z) > acBlock.lan) { 
                        clearListA.Add(i);
                    }else { 
                        //更新到存储
                        acBlock.progress = newPos;
                        activeBlock[i] = acBlock;
                    }
                }
                if (clearListA.Count > 0)
                {
                    // 从后往前移除，避免索引变化
                    for (int i = clearListA.Count - 1; i >= 0; i--)
                    {
                        int index = clearListA[i];
                        if (index >= 0 && index < activeBlock.Count)
                        {
                            activeBlock.RemoveAt(index);
                        }
                    }
                }
            }
        }

        public static void drawBlockBox(Map map,Vector3 doPos,float height,float width,float speed,float lan) { 
            ARMIR_blockBox newBlockBox = new ARMIR_blockBox(
                map,
                doPos,
                doPos,
                height,
                width,
                speed,
                lan
                );
            activeBlock.Add(newBlockBox);
        }

        public struct ARMIR_blockBox { 
            public Map map; //所在地图
            public Vector3 doPos; //发生地点
            public Vector3 progress; //进度
            public float height; //高
            public float width; //宽
            public float speed; //速度
            public float lan; //行进距离

            public ARMIR_blockBox(Map map, Vector3 doPos,Vector3 progress, float height, float width, float speed, float lan)
            {
                this.map = map;
                this.doPos = doPos;
                this.progress = progress;
                this.height = height;
                this.width = width;
                this.speed = speed;
                this.lan = lan;
            }
        }
    }
}
